package com.liao.badminton.service.impl;

import java.util.List;
import java.util.stream.Collectors;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.mapper.SystemSmsLogMapper;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.badminton.entity.SystemSmsLog;
import com.liao.badminton.dto.SystemSmsLogDto;
import com.liao.badminton.vo.SystemSmsLogVo;
import com.liao.badminton.service.SystemSmsLogService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;
import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
/**
* @author liaocheng
* @date 2024-10-30
* description:短信日志
*/
@Component
@Log4j2
public class SystemSmsLogServiceImpl  extends ServiceImpl<SystemSmsLogMapper, SystemSmsLog> implements SystemSmsLogService {


    /**
    * 新增
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Long> saveSystemSmsLog(SystemSmsLogDto param) throws ProjectException{
        SystemSmsLog systemSmsLog = param.invertEntity();
        EntityUtils.setCreatAndUpdateInfo(systemSmsLog);
        this.save(systemSmsLog);
        return ResponseRest.suc("saveSystemSmsLog", systemSmsLog.getId());
    }
    /**
    * 删除
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> deletesSystemSmsLog(SystemSmsLogDto param) throws ProjectException {
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemSmsLog", "id", id));
        }
        return ResponseRest.suc("deletesSystemSmsLog");
    }
    /**
    * 修改
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest<Boolean> updateSystemSmsLog(SystemSmsLogDto param)  throws ProjectException{
        SystemSmsLog systemSmsLog = param.invertEntity();
        EntityUtils.setUpdatedInfo(systemSmsLog);
        boolean bool = this.updateById(systemSmsLog);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "systemSmsLog", "id", param.getId());
        }
        return ResponseRest.suc("updateSystemSmsLog");
    }
    /**
    * 查询列表
    */
    @Override
    public ResponseRest findSystemSmsLogByPageParam(PageParam<SystemSmsLogDto> pageParam)  throws ProjectException{
        CheckUtil.checkValue(pageParam,"pageParam");
        ResponseRest resp = ResponseRest.suc("findSystemSmsLogByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize());
        LambdaQueryWrapper<SystemSmsLog> query = new LambdaQueryWrapper<>();
        List<SystemSmsLog> list = this.list(query);
        List<SystemSmsLogVo> collect = list.stream().parallel().map(entity -> SystemSmsLogVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
    * 查询详情
    */
    @Override
    public ResponseRest findDetail(SystemSmsLogDto param) throws ProjectException {
        Object getDate = RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "systemSmsLog", "id", param.getId());
        if (isNotEmpty(getDate)) {
        RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "systemSmsLog", "id", param.getId());
        return ResponseRest.suc("findDetail", (SystemSmsLogVo) getDate);
        }
        SystemSmsLogVo vo = null;
        SystemSmsLog db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = SystemSmsLogVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA,"systemSmsLog", "id", param.getId(),vo, null);
        }
        return ResponseRest.suc("findDetail", vo);
    }

}
